package projectmanagerapp

import projectmanagerapp.Membership;
import projectmanagerapp.Project;
import projectmanagerapp.User;

class Membership {
	
	User user
	Project project
	
	String toString() {
		"${user.lastName}, ${user.firstName}"
	}
	
	static Membership link(user, project) {
		def m = Membership.findByUserandProject(user, project)
		if (!m)
		{
			m = new Membership()
			user?.addToMember(m)
			project?.addToMember(m)
			m.save()
		}
		return m
	}

	static void unlink(user, project) {
		def m = Membership.findByUserandProject(user, project)
		if (m)
		{
			user?.removeFromMember(m)
			project?.removeFromMember(m)
			m.delete()
		}
	}
    
}
